package pl.edu.agh.inpema.core.environment;

import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;

import pl.edu.agh.inpema.core.components.GlobalAnalyzerUtil;
import pl.edu.agh.inpema.core.components.impl.GlobalAnalyzerUtilImpl;
import pl.edu.agh.inpema.core.configuration.Configuration;
import pl.edu.agh.inpema.core.dataBase.DataBase;
import pl.edu.agh.inpema.core.dataBase.DataBaseFactory;
import pl.edu.agh.inpema.core.model.Analyzer;
import pl.edu.agh.inpema.core.model.GlobalResultsSerializer;
import pl.edu.agh.inpema.core.model.globalAnalyzer.GlobalAnalyzeResult;
import pl.edu.agh.inpema.core.model.globalAnalyzer.GlobalAnalyzer;

public class EnvironmentRunner {
	
	Logger logger = Logger.getLogger(EnvironmentRunner.class);

	public boolean runNewEnvironment(String xmlConf) {
		EnvironmentFactory factory = new EnvironmentFactory();
		Environment environment = null;
		boolean isError = true;
		try {
			
			environment = factory.createNewEnvironment(xmlConf);
			logger.info("environmet configuration done");
			
			environment.startConnection();
			logger.info("connected");
			
			logger.info("starting modules");
			environment.startEnvironment();
			logger.info("modules started");
			
			/*try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}*/
			
			while (environment.isReceiversRunning()) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			isError = false;
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			logger.info("stopping modules");
			environment.stopEnvironment(isError);
			logger.info("modules stopped");
			
			environment.closeConnection();
		}
		
		logger.info("end gathering data");
		return isError;
	}
	
	public void runGlobalAnalyze(String xmlConfFileName, int[] envIds) {
		EnvironmentFactory factory = new EnvironmentFactory();
		Configuration configuration = new Configuration();
		
		DataBase dataBase = null;
		
		try {
			configuration.readXMLFileConfiguration(xmlConfFileName);
			GlobalAnalyzer globalAnalyzer = configuration.getGlobalAnalyzer();
			Set<GlobalResultsSerializer> globalResultSerializers = configuration.getGlobalResultSerializers();
			
			Set<Analyzer> analyzers = new HashSet<Analyzer>();
			for (int envId : envIds) {
				Environment env = factory.createEnvironmentById(envId);
				if (env == null) {
					logger.info("no such environment");
					continue;
				}
				analyzers.addAll(env.getAnalyzers());
			}
			
			dataBase = DataBaseFactory.getDataBaseForEnvironment(analyzers);
			
			for (Analyzer analyzer : analyzers) {
				analyzer.getAnalyzerUtil().setDataBase(dataBase);
			}
			
			GlobalAnalyzerUtil globalAnalyzerUtil = new GlobalAnalyzerUtilImpl(analyzers, dataBase, envIds);
			globalAnalyzer.setGlobalAnalyzerUtil(globalAnalyzerUtil);
			
			GlobalAnalyzeResult globalResults = globalAnalyzer.performAnalyze();

			logger.info("end analyzyng");
			
			for (GlobalResultsSerializer serializer : globalResultSerializers) {
				serializer.serialyzeResults(globalResults);
			}

			logger.info("result serialized");
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dataBase.closeConnection();
		}
	}
	
}
